<?php


namespace app\erp\model;


use think\Db;
use think\Model;

class Pikuandb extends Model
{

    protected $name = 'pikuan';

    protected $pk = 'pk_id';

    public $param = [];

    protected $type = [
        'create_time' => 'timestamp:Y-m-d H:i'
    ];

    protected $append = ['admin_name', 'times_text'];
    // 自动完成时间转换
    protected $autoWriteTimestamp = true;

    public function getTimesTextAttr($value, $row)
    {
        $arr = array(
            '1' => '初始化',
            '2' => '抬头组合',
            '3' => '地址',
            '4' => '手机号',
            '5' => '销项票'
        );
        return $arr[$row['times']];
    }

    public function setDefaultOrderNoAttr($value)
    {
        return json_encode($value);
    }

    public function setOrderNoGroupAttr($value)
    {
        return json_encode($value);
    }

    public function setDefaultGroupAttr($value)
    {
        return json_encode($value);
    }

    public function getDefaultOrderNoAttr($value)
    {
        return json_decode($value, true);
    }

    public function getOrderNoGroupAttr($value)
    {
        $result = json_decode($value, true);
        $return = [];
        if ($result != null && count($result) > 0) {
            foreach ($result as $key => $val) {
                array_push($return, $val);
            }
        }
        return $return;
    }

    public function getDefaultGroupAttr($value)
    {
        return json_decode($value, true);
    }

    public function getAdminNameAttr($value, $row)
    {
        return Db::name('member')->where('member_id', $row['admin_id'])->value('username');
    }

    public function getRenkuanshijianAttr($value)
    {
        return $value == '0000-00-00' ? '' : $value;
    }

    public function base($query)
    {
        $query->where('is_delete', 0);
        if (isset($this->param['order_no']) && $this->param['order_no']) {
            $query->whereLike('order_nos', '%' . $this->param['order_no'] . '%');
        }
        if (isset($this->param['order_type']) && $this->param['order_type'] != '') {
            $query->where('state', $this->param['order_type']);
        }
        if (isset($this->param['zhiyi_state']) && $this->param['zhiyi_state'] != '') {
            $query->where('zhiyi_state', $this->param['zhiyi_state']);
        }
        if(isset($this->param['is_mul']) && $this->param['is_mul'] != ''){
            $query->where('is_mul',$this->param['is_mul']);
        }
        if (isset($this->param['admin_id']) && $this->param['admin_id']) {
            $query->where('admin_id', $this->param['admin_id']);
        }
        if (isset($this->param['view_state']) && $this->param['view_state'] == 'no') {
            $query->where('pk_id', -1);
        }
        if (isset($this->param['return_code']) && $this->param['return_code']) {
            $return_code_arr = explode(',', $this->param['return_code']);
            if (count($return_code_arr) == 1) {
                $query->where('return_code', $this->param['return_code']);
            } else {
                $query->whereIn('return_code', $return_code_arr);
            }
        }
        if (isset($this->param['title']) && $this->param['title']) {
            $query->whereLike('title', '%' . $this->param['title'] . '%');
        }

        if (isset($this->param['min_order_money']) && $this->param['min_order_money']) {
            $query->where('money', '>=', (float)$this->param['min_order_money']);
        }
        if (isset($this->param['max_order_money']) && $this->param['max_order_money']) {
            $query->where('money', '<=', (float)$this->param['max_order_money']);
        }
        if (isset($this->param['order_money']) && $this->param['order_money']) {
            $query->where('money', $this->param['order_money']);
        }
        if (isset($this->param['pay_time_start']) && $this->param['pay_time_start']) {
            $query->whereTime('pay_time', '>=', $this->param['pay_time_start']);
        }
        if (isset($this->param['pay_time_end']) && $this->param['pay_time_end']) {
            $query->whereTime('pay_time', '<=', $this->param['pay_time_end']);
        }
        if (isset($this->param['create_time_start']) && $this->param['create_time_start']) {
            $query->whereTime('renkuanshijian', '>=', strtotime($this->param['create_time_start']));
        }
        if (isset($this->param['create_time_end']) && $this->param['create_time_end']) {
            //$query->whereTime('renkuanshijian', '<=', strtotime('+1 day', strtotime($this->param['create_time_end'])));
            $query->whereTime('renkuanshijian', '<=', strtotime($this->param['create_time_end']));
        }


        if (isset($this->param['runs']) && $this->param['runs'] != '') {
            $query->where('runs', $this->param['runs']);
        }

        if (isset($this->param['times']) && $this->param['times']) {
            $query->where('times', $this->param['times']);
        }
        if (isset($this->param['order_level']) && $this->param['order_level']) {
            $query->where('order_level', $this->param['order_level']);
        }

        if (isset($this->param['zhiyiren']) && $this->param['zhiyiren']) {
            $query->whereLike('zhiyiren', '%' . $this->param['zhiyiren'] . '%');
        }
        if (isset($this->param['dianqiantai']) && $this->param['dianqiantai'] != '') {
            $query->where('dianqiantai', $this->param['dianqiantai']);
        }
        if (isset($this->param['qiantai']) && $this->param['qiantai'] != '') {
            $query->where('qiantai', $this->param['qiantai']);
        }

    }

    public function getOrderAttr($value, $row)
    {
        if ($row['order_nos']) {
            if ($row['order_nos'] == '无') {
                return array(['out_order_no' => '无', 'order_money' => 0, 'supplier_name' => '', 'jun_order_state' => 0,'supplier_name_er'=>'']);
            } else {
                return Db::name('order')
                    ->alias('o')
                    ->join('member m', '(m.member_id=o.supplier_id)', 'LEFT')
                    ->join('member m1','(m1.member_id=o.supplier_er)','LEFT')
                    ->whereIn('out_order_no', $row['order_nos'])
                    ->field('o.out_order_no,o.order_money,m.supplier_name,o.jun_order_state,m1.supplier_name supplier_name_er')
                    ->select();
            }

        } else {
            return array(['out_order_no' => '手动认款', 'order_money' => 0, 'supplier_name' => '', 'jun_order_state' => 0,'supplier_name_er'=>'']);
        }
    }

}